1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.42">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #a71e12}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #a71e12; min-height: 12.0px}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; color: #0000ff}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
}
19 p
.p10
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
; min-height: 17.0px}
20 span
.s1
{font: 12.0px Helvetica
}
21 span
.s2
{color: #0019b7}
22 span
.s3
{font: 9.0px Monaco
}
23 span
.s4
{font: 12.0px Helvetica
; color: #000000}
24 span
.s5
{color: #000000}
25 span
.s6
{color: #606060}
26 span
.s7
{color: #a71e12}
27 span
.s8
{color: #0000ff}
28 span
.Apple-tab-span
{white-space:pre
}
32 <p class=
"p1"><b>Presented in Living Stereo
</b></p>
33 <p class=
"p2"><br></p>
34 <p class=
"p3">Okay, but what about our first, unsimplified example? Remember:
</p>
35 <p class=
"p2"><br></p>
36 <p class=
"p4"><span class=
"s1"><span class=
"Apple-tab-span"> </span></span>{ [
<span class=
"s2">SinOsc
</span>.ar(
440,
0,
0.2),
<span class=
"s2">SinOsc
</span>.ar(
442,
0,
0.2)] }.play;
</p>
37 <p class=
"p5"><span class=
"Apple-tab-span"> </span></p>
38 <p class=
"p3">This also has two SinOscs, but in a different arrangement, between two square brackets
<span class=
"s3">[]
</span>, and with a comma in between. Just like the curly brackets indicate a Function, square brackets define something called an Array. An Array is a type of Collection, which is (you guessed it) a collection of Objects. Collections themselves are Objects, and most types of Collections can hold any types of objects, mixed together, including other Collections! There are many different types of Collections in SC, and you will come to learn that they are one of the SC's most powerful features.
</p>
39 <p class=
"p2"><br></p>
40 <p class=
"p3">An Array is a particular type of Collection: An ordered collection of limited maximum size. You can make one as we have above, by putting objects in between two square brackets, with commas in between. You can get the different elements of an Array using the method 'at', which takes an index as an argument. Indices correspond to the order of objects in the Array, and start from
0.
</p>
41 <p class=
"p2"><br></p>
42 <p class=
"p6"><span class=
"s4"><span class=
"Apple-tab-span"> </span></span><span class=
"s5">a = [
</span><span class=
"s6">"foo"</span><span class=
"s5">,
</span><span class=
"s6">"bar"</span><span class=
"s5">];
<span class=
"Apple-tab-span"> </span></span>//
"foo" is at index
0;
"bar" is at index
1</p>
43 <p class=
"p4"><span class=
"Apple-tab-span"> </span>a.at(
0);
</p>
44 <p class=
"p4"><span class=
"Apple-tab-span"> </span>a.at(
1);
</p>
45 <p class=
"p6"><span class=
"s5"><span class=
"Apple-tab-span"> </span>a.at(
2);
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// returns
"nil", as there is no object at index
2</p>
46 <p class=
"p7"><span class=
"Apple-tab-span"> </span></p>
47 <p class=
"p6"><span class=
"Apple-tab-span"> </span>// there's a shorthand for at that you'll see sometimes:
</p>
48 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s5">a[
0];
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// same as a.at(
0);
</p>
49 <p class=
"p2"><span class=
"Apple-converted-space"> </span></p>
50 <p class=
"p3">In addition to being used to hold collections of objects, Arrays also have a special use in SC: They are used to implement multichannel audio! If your Function returns an Array of UGens (remember that Functions return the result of their last line of code) then the output will be a number of channels. How many depends on the size of the Array, and each channel will correspond to an element of the Array. So in our example:
</p>
51 <p class=
"p2"><br></p>
52 <p class=
"p4"><span class=
"s1"><span class=
"Apple-tab-span"> </span></span>{ [
<span class=
"s2">SinOsc
</span>.ar(
440,
0,
0.2),
<span class=
"s2">SinOsc
</span>.ar(
442,
0,
0.2)] }.play;
</p>
53 <p class=
"p2"><br></p>
54 <p class=
"p3">What we end up with is stereo output, with a SinOsc at
440Hz in the left channel, and a SinOsc at
442Hz in the right channel. We could have even more channels of output by having a larger array.
</p>
55 <p class=
"p2"><br></p>
56 <p class=
"p3">Now watch carefully, because this next bit involves a little slight of hand, but shows another way in which SC makes things very interchangeable. Because the arguments for phase and mul are the same for both SinOscs, we can rewrite the code for our example like this:
</p>
57 <p class=
"p2"><br></p>
58 <p class=
"p4"><span class=
"s1"><span class=
"Apple-tab-span"> </span></span>{
<span class=
"s2">SinOsc
</span>.ar([
440,
442],
0,
0.2) }.play;
</p>
59 <p class=
"p2"><br></p>
60 <p class=
"p3">We've replaced the frequency argument with an Array. This causes something called 'multichannel expansion', which means that if you plug an Array into one of a UGen's arguments, you get an Array of that UGen instead of a single one. Now consider this:
</p>
61 <p class=
"p2"><br></p>
62 <p class=
"p4"><span class=
"s1"><span class=
"Apple-tab-span"> </span></span>(
</p>
63 <p class=
"p4"><span class=
"Apple-tab-span"> </span>{ var freq;
</p>
64 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>freq = [[
660,
880], [
440,
660],
1320,
880].choose;
</p>
65 <p class=
"p4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s2">SinOsc
</span>.ar(freq,
0,
0.2);
<span class=
"Apple-converted-space"> </span></p>
66 <p class=
"p4"><span class=
"Apple-tab-span"> </span>}.play;
</p>
67 <p class=
"p4"><span class=
"Apple-tab-span"> </span>)
</p>
68 <p class=
"p2"><br></p>
69 <p class=
"p3">Try executing it several times, and you'll get different results. 'choose' is just a method which randomly selects one of the elements of the Array. In this case the result may be a single number or another Array. In the case of the latter you'll get stereo output, in the case of the former, monophonic. This sort of thing can make your code very flexible.
</p>
70 <p class=
"p2"><br></p>
71 <p class=
"p3">But what if you want to 'pan' something, crossfading it between channels? SC has a number of UGens which do this in various ways, but for now I'll just introduce you to one: Pan2. Pan2 takes an input and a position as arguments and returns an Array of two elements, the left and right or first and second channels. The position arg goes between -
1 (left) and
1 (right). Take a look at this example:
</p>
72 <p class=
"p2"><br></p>
73 <p class=
"p4"><span class=
"Apple-tab-span"> </span>{
<span class=
"s2">Pan2
</span>.ar(
<span class=
"s2">PinkNoise
</span>.ar(
0.2),
<span class=
"s2">SinOsc
</span>.kr(
0.5)) }.play;
</p>
74 <p class=
"p2"><br></p>
75 <p class=
"p3">This uses a SinOsc to control the position (remember it outputs values from -
1 to
1, or left to right), but uses a different UGen as the input to the Pan2, something called PinkNoise. This is just a kind of noise generator, and it has a single argument: mul. You can of course also used fixed values for the position arg.
</p>
76 <p class=
"p2"><br></p>
77 <p class=
"p4"><span class=
"Apple-tab-span"> </span>{
<span class=
"s2">Pan2
</span>.ar(
<span class=
"s2">PinkNoise
</span>.ar(
0.2), -
0.3) }.play;
<span class=
"s7">// slightly to the left
</span></p>
78 <p class=
"p5"><span class=
"Apple-tab-span"> </span></p>
79 <p class=
"p3">For more information see:
<b><span class=
"Apple-converted-space"> </span></b></p>
80 <p class=
"p2"><br></p>
81 <p class=
"p8"><a href=
"../../Other Topics/MultiChannel.html">MultiChannel
</a><span class=
"s5"><b> </b><a href=
"../../Collections/Collections.html"><span class=
"s1">Collections
</span></a><b> </b><a href=
"../../UGens/Multichannel/Panners/Pan2.html"><span class=
"s1">Pan2
</span></a></span></p>
82 <p class=
"p2"><br></p>
83 <p class=
"p9"><b>Suggested Exercise:
</b></p>
84 <p class=
"p10"><br></p>
85 <p class=
"p3">Experiment with altering the Functions in the text above. For instance try changing the frequencies of the SinOsc, or making multi-channel versions of things.
</p>
86 <p class=
"p2"><br></p>
87 <p class=
"p3">____________________
</p>
88 <p class=
"p2"><br></p>
89 <p class=
"p3">This document is part of the tutorial
<b>Getting Started With SuperCollider
</b>.
</p>
90 <p class=
"p2"><br></p>
91 <p class=
"p3">Click here to go on to the next section:
<a href=
"Mix it Up.html"><span class=
"s8">Mix it Up
</span></a></p>
92 <p class=
"p2"><br></p>
93 <p class=
"p3">Click here to return to the table of Contents:
<a href=
"Getting Started With SC.html"><span class=
"s8">Getting Started With SC
</span></a></p>